<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="orderInfo.query"
            class="input-with-select"
            clearable
            @clear="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单表格区域 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column align="center" type="index" label="#"></el-table-column>
        <el-table-column align="center" prop="order_number" label="订单编号"></el-table-column>
        <el-table-column align="center" prop="order_price" label="订单价格"></el-table-column>
        <el-table-column align="center" prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-tag type="danger" plain v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" plain v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="is_send" label="是否发货"></el-table-column>
        <el-table-column align="center" prop="create_time" label="下单时间"></el-table-column>
        <el-table-column align="center" prop="address" label="操作">
          <template>
            <el-button type="primary" icon="el-icon-edit" plain @click="showAddress"></el-button>
            <el-button
              type="success"
              icon="el-icon-location-outline"
              plain
              @click="showInformation"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="orderInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改对话框 -->
    <el-dialog title="修改地址" :visible.sync="addressDialog" width="50%" @close="addressDialogClear">
      <!-- 表单验证区域 -->
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address">
          <el-cascader v-model="addressForm.address" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address1">
          <el-input v-model="addressForm.address1"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialog = false">取 消</el-button>
        <el-button type="primary" @click="addressDialog = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 地址对话框 -->
    <el-dialog title="物流进度" :visible.sync="processDialog" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in processList"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="processDialog = false">取 消</el-button>
        <el-button type="primary" @click="processDialog = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  data () {
    return {
      cityData: cityData,
      // 定义一个订单的列表的请求数据
      orderInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },

      // 订单数据接收数据方法
      orderList: [],

      // 总数
      total: 0,

      // 修改地址对话框的显示和隐藏
      addressDialog: false,

      // 地址绑定表单
      addressForm: {
        address: [],
        address1: ''
      },

      // 地址验证规则
      addressFormRules: {
        address: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        address1: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },

      // 物流信息对话框
      processDialog: false,

      // 物流信息数据
      processList: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据方法
    async getOrderList () {
      const { data: res } = await this.$http.get('/orders', { params: this.orderInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      res.data.goods.forEach(item => {
        item.create_time = this.$dayjs(item.create_time).format('YYYY-MM-DD hh:mm')
      })
      this.orderList = res.data.goods
      this.total = res.data.total
      // console.log(res.data)
    },

    // 显示每页条数的方法
    handleSizeChange (newSize) {
      this.orderInfo.pagesize = newSize
      this.getOrderList()
    },

    // 显示当前页码
    handleCurrentChange (newPage) {
      this.orderInfo.pagenum = newPage
      this.getOrderList()
    },

    // 点击修改按钮弹出对话框
    showAddress () {
      this.addressDialog = true
    },

    // 清空地址验证
    addressDialogClear () {
      this.$refs.addressFormRef.resetFields()
    },

    // 物流信息点击事件
    async showInformation () {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.processList = res.data
      console.log(res.data)
      this.processDialog = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
